package cn.itcast.bassm.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.itcast.bassm.mapper.UserMapper;
import cn.itcast.bassm.message.MusicMessage;
import cn.itcast.bassm.message.NewsMessage;
import cn.itcast.bassm.message.TextMessage;
import cn.itcast.bassm.util.Article;
import cn.itcast.bassm.util.MessageUtil;
import cn.itcast.bassm.util.Music;
	


@Service
public class CoreService {
	
	@Autowired
	private UserMapper userMapper;
	
	
	public  String processRequest(HttpServletRequest request){
		String respXML= null;
		TextMessage tm = new TextMessage();
		//解析微信服务器发送请求
		try {
			HashMap<String, String>requestMap = MessageUtil.parseXML(request);
			//微信用户的openid
			String fromUserName = requestMap.get("FromUserName");
			String openid=fromUserName;
			//公众号的原始ID
			String toUserName = requestMap.get("ToUserName");
			//请求类型
			String msgType = requestMap.get("MsgType");
			//还可以取其他参数***
			//临时参数 卡号
			String ka=requestMap.get("EventKey");
			String tt="qrscene_";
			String cardnumber=ka.replaceAll(tt, "");
			System.out.println("==============================>"+cardnumber);
			System.out.println("==============================>"+cardnumber);
			System.out.println("==============================>"+cardnumber);
			System.out.println("==============================>"+cardnumber);					
			tm.setFromUserName(toUserName);
			tm.setToUserName(fromUserName);
			tm.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
			tm.setCreateTime(new Date().getTime());
			userMapper.insetPublicandNumber(openid, cardnumber);
			//判断用户发送的是那一种类型的消息
			//文本消息
			if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
				String content=requestMap.get("Content");
				//回复网页超链接
				//tm.setContent("点击查看<a href=\"http://wwwl.ycws.cc/bassm/index1.jsp\">页面跳转</a>");
				//tm.setContent("你发送的是文本消息");
				if("音乐".equals(content)){
					// 创建音乐对象
					Music music = new Music();
					music.setTitle("旅行");
					music.setDescription("许巍");
					music.setMusicUrl("http://wwl.ycws.cc/ssm/music/chengdu.mp3");	//手机网络 2g/3g/4g
					music.setHQMusicUrl("http://wwl.ycws.cc/ssm/music/chengdu.mp3");//高清品质 WIFI
					
					// 创建音乐消息
					MusicMessage musicMessage = new MusicMessage();
					musicMessage.setFromUserName(toUserName);
					musicMessage.setToUserName(fromUserName);
					musicMessage.setCreateTime(new Date().getTime());
					musicMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_MUSIC);
					musicMessage.setMusic(music);
					
					respXML = MessageUtil.messageToXML(musicMessage);

				}
				else if ("7".equals(content)) {
					//回复图文消息,没有图片 最多10张
					Article article = new Article();
					article.setItem("后台管理");
					article.setDescription("可以进行用户的授权");
					article.setPicUrl("");//图片链接
					article.setUrl("http://wwl.ycws.cc/bassm/index1.jsp");
					
					List<Article> articleList = new ArrayList<Article>();
					articleList.add(article);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXML = MessageUtil.messageToXML(newsMessage);
					
				}else if ("8".equals(content)) {
					//回复多图文消息，有图片
					Article article = new Article();
					article.setItem("后台管理");
					article.setDescription("可以进行用户的授权");
					article.setPicUrl("http://wwl.ycws.cc/ssm/music/eanquan.jpg");//图片链接
					article.setUrl("http://wwl.ycws.cc/bassm/index1.jsp");
					
					List<Article> articleList = new ArrayList<Article>();
					articleList.add(article);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXML = MessageUtil.messageToXML(newsMessage);
					
				}else if ("9".equals(content)) {
					//回复图文消息 多图文
					Article article1 = new Article();
					article1.setItem("后台管理");
					article1.setDescription("可以进行用户的授权");
					article1.setPicUrl("http://wwl.ycws.cc/ssm/music/eanquan.jpg");//图片链接
					article1.setUrl("http://wwl.ycws.cc/bassm/index1.jsp");
					
					
					Article article2 = new Article();
					article2.setItem("后台管理2");
					article2.setDescription("可以进行用户的授权2");
					article2.setPicUrl("http://wwl.ycws.cc/ssm/music/eanquan.jpg");//图片链接
					article2.setUrl("http://wwl.ycws.cc/bassm/index1.jsp");
					
					List<Article> articleList = new ArrayList<Article>();
					articleList.add(article1);
					articleList.add(article2);
					
					NewsMessage newsMessage = new NewsMessage();
					newsMessage.setFromUserName(toUserName);
					newsMessage.setToUserName(fromUserName);
					newsMessage.setCreateTime(new Date().getTime());
					newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
					newsMessage.setArticleCount(articleList.size());
					newsMessage.setArticles(articleList);
					
					respXML = MessageUtil.messageToXML(newsMessage);
				}else {
					tm.setContent("你发送的是文本消息");
					respXML = MessageUtil.messageToXML(tm);
				}
			}
			//图片消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
				tm.setContent("你发送的是图片消息");
				respXML = MessageUtil.messageToXML(tm);
			}
			//连接消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
				tm.setContent("你发送的是连接消息");
				respXML = MessageUtil.messageToXML(tm);
			}
			//地理位置消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
				tm.setContent("你发送的是地理位置消息");
				respXML = MessageUtil.messageToXML(tm);
			}
			//视频消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VIDEO)) {
				tm.setContent("你发送的是视频消息");
				respXML = MessageUtil.messageToXML(tm);
			}
			//语音消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
				tm.setContent("你发送的是语音消息");
				respXML = MessageUtil.messageToXML(tm);
			}
			//事件消息
			else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
				//事件类型
				String eventType = requestMap.get("Event");
				//关注事件
				if(eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){
					tm.setContent("欢迎关注");
					respXML = MessageUtil.messageToXML(tm);
					
				}
				//取消事件
				else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
					
				}
			}
			
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
		//判断：对不同的消息请求回复不同的内容
		
		return respXML;
	}
}
